<?php
$loader->requireOnce('includes/LabResult/LabItemFactorModel.class.php');
/**
 * lab model 
 * @package modules.dept_laboratory.includes
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabAssignedItemModel {
	private $listOfLabItemFactorModel = array();
	private $labAssignedItemOrdo;
	private $done;
	function __construct($labAssignedItemId) {
		$this->done = false;
		$this->labAssignedItemOrdo = Celini::newOrdo('LabAssignedItem');
        $this->labAssignedItemOrdo->setup($labAssignedItemId);
        $this->buildLabItemFactorModel($labAssignedItemId);
        		
	}
	public function isDone() {
		return $this->done;
	}
	public function getLabAssignedItemOrdo() {
		return $this->labAssignedItemOrdo;
	}	
	public function getLabItemsFactorModel() {
		return $this->listOfLabItemFactorModel;
	}
	private function buildLabItemFactorModel($labAssignedItemId) {
		$labResultObj = Celini::newOrdo('LabResult');
        $listOfItems = $labResultObj->listLabResultItems($labAssignedItemId);
        foreach ($listOfItems as $item) {
            //add into array
             $fmItem = new LabItemFactorModel();
             $fmItem->fillDataForDisplay($item['lab_result_id'], $item['name'], $item['result'], $item['normal_result_male'],
             $item['normal_result_female'], $item['enum_data_type'], $item['enum_unit']);
             $this->listOfLabItemFactorModel[$item['lab_result_id']] = $fmItem;
        }
	}
	
	public function saveData($resultData) {
		if (!is_array($resultData )) {
			return false;
		}
		$this->done = false	;	
		foreach ($this->listOfLabItemFactorModel as &$item) {
			
			if (isset($resultData[$item->getLabResultId()])) {
				//at least one result inputed then this assigned item is done.
				if ($resultData[$item->getLabResultId()] != null 
				&& trim($resultData[$item->getLabResultId()]) != '') {
					$this->done = true;
				}
				$item->saveResult($resultData[$item->getLabResultId()]);				
			}
						
		}
		$this->labAssignedItemOrdo->setDone($this->done);
	}	
	
}
?>